<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var obj = {method: 'get', url: './01.json', date: 'username=10&age=20'}
 ajaxObj(obj)
        function ajaxObj(obj) {
        console.log(obj)
            //1.创建XMLHttpRequest对象
            var xhr = new XMLHttpRequest()
            
            //2.打开
            if (obj.method.toLocaleLowerCase() == 'get') {
                //判断有没有参数
                if (obj.data) {
                    obj.url = obj.url + '?' + obj.data
                }
                xhr.open(obj.method, obj.url, true)
                //3.发送
                xhr.send()
            } else {
                xhr.open(obj.method, obj.url, true)
                //设置请求头
                xhr.setRequestHeader('Content-type', "application/x-www-form-urlencoded")
                //发送
                if (obj.data) {
                    xhr.send(obj.data)
                } else {
                    xhr.send()
                }
            }
            //4.注册监听事件
            xhr.onreadystatechange = function () {
                //5.监听ajax==4    服务器==200
                if (xhr.readyState == 4 && xhr.status == 200) {
                    //可以获取到数据，正常渲染页面
                    // console.log(JSON.parse(xhr.responseText));
                    obj.success(JSON.parse(xhr.responseText))
                }
            }
        }
    </script>
</body>
</html>